<template>
  <div class="app-container">
    <div slot="footer" class="dialog-footer" style="margin-bottom: 30px">
      <el-button type="success" icon="el-icon-check" style="width: 140px;float: right" @click="optCmd === 'edit'?editData():createData()">资讯提交</el-button>
    </div>
    <el-form ref="formData" v-loading="formLoading" :model="formData" :rules="rules" size="small" label-width="120px">
      <el-form-item label="文章类型 " prop="articleType">
        <el-select v-model="formData.articleType" filterable placeholder="请选择">
          <el-option v-for="item in dict.dict.type_article" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="文章标题" prop="articleTitle">
        <el-input v-model="formData.articleTitle" />
      </el-form-item>
      <el-form-item label="文章副标题">
        <el-input v-model="formData.articleSubtitle" />
      </el-form-item>
      <el-form-item label="文章封面" prop="articleCoverPic">
        <el-row>
          <el-col :span="22"><el-input v-model="formData.articleCoverPic" /></el-col>
          <el-col :span="2"><pic-selector oss-path="article/coverimg" color="#1890ff" @successCBK="imageSuccessCBK" /></el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="文章作者" prop="articleAuthor">
        <el-input v-model="formData.articleAuthor" />
      </el-form-item>
      <el-form-item label="文章来源" prop="articleSource">
        <el-input v-model="formData.articleSource" />
      </el-form-item>
      <el-form-item label="文章简介">
        <el-input v-model="formData.artcleIntroduction" type="textarea" />
      </el-form-item>
      <el-form-item label="文章内容" prop="articleContent">
        <tinymce v-model="formData.articleContent" :height="800" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce/index'
import { add, edit, getArticleById } from '@/api/business/common/article/article-list'
import picSelector from '@/components/PicSelector/picSelector'

export default {
  name: 'ArticleEdit',
  components: { Tinymce, picSelector },
  dicts: ['type_article'],
  data() {
    return {
      limitCount: 1,
      optCmd: 'create',
      formLoading: false,
      formData: {
        articleTitle: '',
        articleSubtitle: '',
        articleContent: '',
        artcleIntroduction: '',
        articleAuthor: '',
        articleSource: '',
        articleType: undefined,
        articleCoverPic: ''
      },
      rules: {
        articleTitle: [{ required: true, message: '文章标题不能为空', trigger: 'blur' }],
        articleContent: [{ required: true, message: '文章内容不能为空', trigger: 'blur' }],
        articleType: [{ required: true, message: '文章类型 不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.optCmd = this.$route.query.opt
    if (this.optCmd === 'edit') {
      this.getArticleInfo(this.$route.query.id)
    }
  },
  methods: {
    imageSuccessCBK(arr) {
      const _this = this
      arr.forEach(v => {
        _this.formData.articleCoverPic = v.relativePath
      })
    },
    createData() {
      this.$refs['formData'].validate((valid) => {
        if (valid) {
          this.formLoading = true
          add(this.formData).then(() => {
            this.formLoading = false
            this.$notify({
              title: 'Success',
              message: '添加成功',
              type: 'success',
              duration: 2000
            })
            this.$router.push({ path: '/article/list' })
          }).catch(() => {
            this.formLoading = false
          })
        }
      })
    },
    getArticleInfo(articleId) {
      getArticleById(articleId).then((response) => {
        Object.assign(this.formData, response)
      })
    },
    editData() {
      this.$refs['formData'].validate((valid) => {
        if (valid) {
          this.formLoading = true
          edit(this.formData).then(() => {
            this.formLoading = false
            this.$notify({
              title: 'Success',
              message: '编辑成功',
              type: 'success',
              duration: 2000
            })
            this.$router.push({ path: '/article/list' })
          }).catch(() => {
            this.formLoading = false
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
